package edu.buaa.act.travel.m.adapter;

import java.text.SimpleDateFormat;
import java.util.List;


import edu.buaa.act.travel.m.R;
import edu.buaa.act.travel.m.tpdl.ResortActivity;
import edu.buaa.act.travel.m.tpdl.TpdlActivity;
import edu.buaa.act.travel.m.tpdl.TransitActivity;
import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class TripDetailAdapter extends BaseAdapter {
	
	private List<TpdlActivity> mTripList;
	private Activity mActivity;
	private SimpleDateFormat dFormat;
	
	public TripDetailAdapter(final Activity act, final List<TpdlActivity> trips) {
		mActivity = act;
		mTripList = trips;
		dFormat = new SimpleDateFormat();
	}

	@Override
	public int getCount() {
		return mTripList.size();
	}

	@Override
	public Object getItem(int position) {
		return mTripList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(convertView == null) {
			convertView = mActivity.getLayoutInflater().inflate(R.layout.item_trip_detail, null);
		}
		
		final TpdlActivity item = mTripList.get(position);
		final ImageView dispIcon = (ImageView) convertView.findViewById(R.id.image_item);
		final TextView dispName = (TextView) convertView.findViewById(R.id.text_trip);
		final TextView dispAddr = (TextView) convertView.findViewById(R.id.text_address);
		final TextView dispStartTime = (TextView) convertView.findViewById(R.id.text_start_time);
		if(item instanceof ResortActivity){
			dispIcon.setImageResource(R.drawable.secenery);
			dispName.setText(item.getName());
			dispAddr.setText(item.getAddress());
			dispStartTime.setText(dFormat.format(item.getStartTime()));
		}
		else if (item instanceof TransitActivity){
			dispIcon.setImageResource(R.drawable.transpot);
			dispName.setText(item.getName());
			dispStartTime.setText(dFormat.format(item.getStartTime()));
			dispAddr.setText(((TransitActivity)item).getType());
		}
		
		return convertView;
	}

	public List<TpdlActivity> getTripDetails() {
		return mTripList;
	}
}
